package cn.com.yocat.flowable.config;

import org.flowable.ui.common.repository.UuidIdGenerator;
import org.flowable.ui.common.tenant.TenantProvider;
import org.flowable.ui.modeler.domain.Model;
import org.flowable.ui.modeler.repository.ModelRepository;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ModelRepositoryImpl implements ModelRepository {
    private static final String NAMESPACE = "org.flowable.ui.modeler.domain.Model.";
    @Autowired
    protected SqlSessionTemplate sqlSessionTemplate;
    @Autowired
    protected UuidIdGenerator idGenerator;
    @Autowired
    protected TenantProvider tenantProvider;

    public ModelRepositoryImpl() {
    }

    public Model get(String id) {
        return (Model) this.sqlSessionTemplate.selectOne("org.flowable.ui.modeler.domain.Model.selectModel", id);
    }

    public List<Model> findByModelType(Integer modelType, String sort) {
        Map<String, Object> params = new HashMap();
        params.put("modelType", modelType);
        params.put("sort", sort);
        return this.findModelsByParameters(params);
    }

    public List<Model> findByModelTypeAndFilter(Integer modelType, String filter, String sort) {
        Map<String, Object> params = new HashMap();
        params.put("modelType", modelType);
        params.put("filter", filter);
        params.put("sort", sort);
        return this.findModelsByParameters(params);
    }

    public List<Model> findByKeyAndType(String key, Integer modelType) {
        Map<String, Object> params = new HashMap();
        params.put("key", key);
        params.put("modelType", modelType);
        return this.findModelsByParameters(params);
    }

    public List<Model> findByParentModelId(String parentModelId) {
        return this.sqlSessionTemplate.selectList("org.flowable.ui.modeler.domain.Model.selectModelByParentModelId", parentModelId);
    }

    public Long countByModelTypeAndCreatedBy(int modelType, String createdBy) {
        Map<String, Object> params = new HashMap();
        params.put("createdBy", createdBy);
        params.put("modelType", modelType);
        params.put("tenantId", this.tenantProvider.getTenantId());
        return (Long) this.sqlSessionTemplate.selectOne("org.flowable.ui.modeler.domain.Model.countByModelTypeAndCreatedBy", params);
    }

    protected List<Model> findModelsByParameters(Map<String, Object> parameters) {
        parameters.put("tenantId", this.tenantProvider.getTenantId());
        return this.sqlSessionTemplate.selectList("org.flowable.ui.modeler.domain.Model.selectModelByParameters", parameters);
    }

    public void save(Model model) {
        if (model.getId() == null) {
            model.setId(this.idGenerator.generateId());
            model.setTenantId(this.tenantProvider.getTenantId());
            this.sqlSessionTemplate.insert("org.flowable.ui.modeler.domain.Model.insertModel", model);
        } else {
            this.sqlSessionTemplate.update("org.flowable.ui.modeler.domain.Model.updateModel", model);
        }

    }

    public void delete(Model model) {
        this.sqlSessionTemplate.delete("org.flowable.ui.modeler.domain.Model.deleteModel", model);
    }
}

